{{extend './login.html'}} {{block 'content'}}
<h3 class="header">用户注册</h3>
<form id="register_form" method="post" action="/register">
  <div class="form-group">
    <label for="email">邮箱</label>
    <input type="email" class="form-control" id="email" name="email" placeholder="Email" autofocus />
  </div>
  <div class="form-group">
    <label for="nickname">昵称</label>
    <input type="text" class="form-control" id="nickname" name="nickname" placeholder="Nickname" />
  </div>
  <div class="form-group">
    <label for="password">密码</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Password" />
  </div>
  <button type="submit" class="btn btn-info btn-block">注册</button>
</form>
<div class="message">
  <p>已有账号? <a href="/login">点击登录</a>.</p>
</div>
{{/block}} {{block 'script'}}

<script>
  $("#register_form").on("submit", (e) => {
    e.preventDefault();
    const formData = $("#register_form").serialize();
    $.ajax({
      url: "/login/register",
      type: "POST",
      data: formData,
      dataType: "json", // 返回的json数据解析为对象
      success: (data) => {
        if (data.err_code === 0) {
          window.alert("注册成功");
          window.location.href = "/";
        } else if (data.err_code === 1) {
          window.alert("邮箱或密码已存在");
        } else if (data.err_code === 500) {
          window.alert("服务器繁忙，请刷新或者等待后重试");
        }
      },
    });
  });
</script>
{{/block}}